<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="影音分类" prop="thirdColumnCategoryId">
        <el-select v-model="queryParams.thirdColumnCategoryId" placeholder="所属影音分类" clearable size="small">
          <el-option
            v-for="item in thirdColumnCategoryOptions"
            :key="item.thirdColumnCategoryId"
            :label="item.thirdColumnCategoryName"
            :value="item.thirdColumnCategoryId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="面额" prop="money">
        <el-input
          v-model="queryParams.money"
          placeholder="请输入金额"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['app:product:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['app:product:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['app:product:remove']"
        >删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="productList" @selection-change="handleSelectionChange" tableId="thirdProduct">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="序号" type="index" width="50" align="center">
        <template slot-scope="scope">
          <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="商品类型" align="center" prop="productType" :formatter="productTypeFormat"/>
      <el-table-column label="影音" align="center" prop="thirdColumnName"/>
      <el-table-column label="影音分类" align="center" prop="thirdColumnCategoryName"/>
      <el-table-column label="商品名称" align="center" prop="mobileFuluProductName"/>
      <el-table-column label="划线价" align="center" prop="money"/>
      <el-table-column label="成本价" align="center" prop="mobileMoneyCost"/>
      <el-table-column label="售价" align="center" prop="mobileMoneyPrice"/>
      <el-table-column label="排序" align="center" prop="productSort"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['app:product:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['app:product:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改话费商品对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="950px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="商品类型" prop="productType">
              <el-select v-model="form.productType" placeholder="请选择商品类型" :disabled="true" clearable size="small">
                <el-option
                  v-for="product in productTypeOptions"
                  :key="product.value"
                  :label="product.label"
                  :value="product.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="所属权益" prop="thirdColumnId">
              <el-select v-model="form.thirdColumnId" placeholder="请选择所属权益" :disabled="true" clearable size="small">
                <el-option
                  v-for="item in thirdColumnOptions"
                  :key="item.thirdColumnId"
                  :label="item.thirdColumnName"
                  :value="item.thirdColumnId"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="权益分类" prop="productType">
              <el-select v-model="form.thirdColumnCategoryId" placeholder="所属权益分类" clearable size="small">
                <el-option
                  v-for="item in thirdColumnCategoryOptions"
                  :key="item.thirdColumnCategoryId"
                  :label="item.thirdColumnCategoryName"
                  :value="item.thirdColumnCategoryId"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="商品名称" prop="mobileFuluProductName">
              <el-input v-model="form.mobileFuluProductName" placeholder="请输入商品名称"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="划线价" prop="money">
              <el-input-number v-model="form.money" :min="10" :max="100000" :step="10" :step-strictly="true"
                               placeholder="请输入划线价"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="成本价" prop="mobileMoneyCost">
              <el-input v-model="form.mobileMoneyCost" placeholder="请输入成本价"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="售价" prop="mobileMoneyPrice">
              <el-input v-model="form.mobileMoneyPrice" placeholder="请输入售价"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="产品Id" prop="mobileFuluProductId">
              <el-input v-model="form.mobileFuluProductId" placeholder="请输入微云集产品Id"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-form-item label="排序" prop="productSort">
            <el-input-number v-model="form.productSort" :min="1" :max="100000" :step="1" :step-strictly="true"
                             placeholder="请输入排序"/>
          </el-form-item>
          <el-form-item label="商品图片" prop="productImg">
            <el-upload
              class="avatar-uploader"
              :action="upload.url"
              :show-file-list="false"
              :headers="upload.headers"
              name="file"
              accept=".jpg, .png"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload">
              <img v-if="form.productImg" :src="form.productImg | imgPathFilter" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
        </el-row>
        <el-row>
          <el-col :span="24">

            <el-form-item label="商品描述" prop="productDesc">
              <el-input v-model="form.productDesc" type="textarea" :rows="6" placeholder="请输商品描述"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getToken } from '@/utils/auth'
import { queryListCategory } from '@/api/app/thirdColumnCategory'
import { queryListColumn } from '@/api/app/thirdColumn'
import { addProduct, delProduct, getProduct, listProduct1, updateProduct } from '@/api/app/product'

export default {
  props: {
    thirdColumnId: '',
    productType: ''
  },
  name: 'Product',
  components: {},
  data() {
    return {
      // 上传参数
      upload: {
        // 是否禁用上传
        isUploading: false,
        // 设置上传的请求头部
        headers: { Authorization: 'Bearer ' + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + '/common/upload',
        // 上传的文件列表
        fileList: []
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      productTypeOptions: [
        { value: 11, label: '影视会员' },
        { value: 12, label: '音乐会员' },
        { value: 13, label: '在线教育' },
        { value: 16, label: '交通出行' },
        { value: 17, label: '美食饮品' },
        { value: 18, label: '酒店旅游' }
      ],
      // 话费商品表格数据
      productList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productTypes: '11,12,13,14,15,16,17,18',
        productType: null,
        money: null,
        thirdColumnId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        mobileFuluProductName: [
          { required: true, message: '商品名称不能为空', trigger: 'blur' }
        ],
        money: [
          { required: true, message: '面额不能为空', trigger: 'blur' }
        ],
        mobileMoneyCost: [
          { required: true, message: '成本价不能为空', trigger: 'blur' }
        ],
        mobileMoneyPrice: [
          { required: true, message: '售价不能为空', trigger: 'blur' }
        ]
      },
      thirdColumnCategoryOptions: [],
      thirdColumnOptions: []
    }
  },
  created() {
    this.queryParams.thirdColumnId = this.thirdColumnId
    this.getList()
    this.getListCategory()
    this.getListColumn()
  },
  methods: {
    productTypeFormat(row, column) {
      if (row.productType == 11) {
        return '影视会员'
      }
      if (row.productType == 12) {
        return '音乐会员'
      }
      if (row.productType == 13) {
        return '在线教育'
      }
      if (row.productType == 14) {
        return '肯德基'
      }
      if (row.productType == 15) {
        return '麦当劳'
      }
     if (row.productType == 17) {
        return '美食饮品'
     }
        if (row.productType == 16) {
            return '交通出行'
        }
     if (row.productType == 18) {
        return '酒店旅游'
     }
     },
    /** 查询话费商品列表 */
    getList() {
      this.loading = true
      listProduct1(this.queryParams).then(response => {
        this.productList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 查询分类列表 */
    getListCategory() {
      queryListCategory({ thirdColumnId: this.thirdColumnId }).then(response => {
        this.thirdColumnCategoryOptions = response.data
      })
    },
    /** 查询影音列表 */
    getListColumn() {
      queryListColumn({}).then(response => {
        this.thirdColumnOptions = response.data
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        billId: null,
        money: null,
        mobileMoneyCost: null,
        mobileMoneyPrice: null,
        mobileFuluProductId: null,
        mobileFuluProductName: null,
        productImg: null,
        productDesc: null,
        productSort: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        unicomMoneyCost: null,
        unicomMoneyPrice: null,
        unicomFuluProductId: null,
        unicomFuluProductName: null,
        telecomMoneyCost: null,
        telecomMoneyPrice: null,
        telecomFuluProductId: null,
        telecomFuluProductName: null,
        remark: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.billId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.form.thirdColumnId = Number(this.thirdColumnId)
      this.form.productType = Number(this.productType)
      this.open = true
      this.title = '添加商品'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const billId = row.billId || this.ids
      getProduct(billId).then(response => {
        this.form = response.data
        console.info(this.form)
        this.open = true
        this.title = '修改商品'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.billId != null) {
            updateProduct(this.form).then(response => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addProduct(this.form).then(response => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const billIds = row.billId || this.ids
      this.$confirm('是否确认删除商品编号为"' + billIds + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delProduct(billIds)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      })
    },
    handleAvatarSuccess(res, file) {
      this.form.productImg = res.data.objectID
    },
    beforeAvatarUpload(file) {
      const isLt5M = file.size / 1024 / 1024 < 5
      if (!isLt5M) {
        this.$message.error('上传导航图标大小不能超过 5MB!')
      }
      return isLt5M
    }
  }
}
</script>
